/*******************************************************************************
 * Copyright 2011 Mauro Luigi Drago (drago.luigi@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.bytenose.extrautils.logging;

import java.io.OutputStream;

import com.bytenose.extrautils.annotations.NonNullable;

/**
 * An error aware logger using a stream to log messages.
 * @author Mauro Luigi Drago
 *
 * @param ErrorType the type of errors to log.
 */
public final class StreamErrorLogger<ErrorType> implements ILogger, IStreamLogger, IErrorLogger<ErrorType> {
	/** The nested logger */
	private final IStreamLogger nestedLogger;
	
	/** The nested error logger */
	private final ForwardingErrorLogger<ErrorType> nestedErrorLogger;
	
	/**
	 * Creates a new {@link StreamErrorLogger} with auto flushing.
	 * 
	 * @param os the logging stream, cannot be null.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	public StreamErrorLogger(@NonNullable OutputStream os) 
			throws IllegalArgumentException {
		if (os == null) throw new IllegalArgumentException("os");
		nestedLogger = new StreamLogger(os, true);
		nestedErrorLogger = ForwardingErrorLogger.create(nestedLogger, true);
	}
	
	/**
	 * Creates a new {@link StreamErrorLogger}.
	 * 
	 * @param os the logging stream, cannot be null.
	 * @param autoFlush 
	 * 		a flag indicating if messages should be immediately
	 * 		flushed when received.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	public StreamErrorLogger(@NonNullable OutputStream os, boolean autoFlush) 
			throws IllegalArgumentException {
		if (os == null) throw new IllegalArgumentException("os");
		nestedLogger = new StreamLogger(os, autoFlush);
		nestedErrorLogger = ForwardingErrorLogger.create(nestedLogger, true);
	}
	
	/**
	 * Creates a new {@link StreamErrorLogger} with auto flushing.
	 * 
	 * @param <T> the type of errors.
	 * @param os the logging stream, cannot be null.
	 * @return the new logger.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	@NonNullable 
	public static <T> StreamErrorLogger<T> create(@NonNullable OutputStream os) 
			throws IllegalArgumentException {
		return new StreamErrorLogger<T>(os);
	}
	
	/**
	 * Creates a new {@link StreamErrorLogger}.
	 * 
	 * @param os the logging stream, cannot be null.
	 * @param autoFlush 
	 * 		a flag indicating if messages should be immediately
	 * 		flushed when received.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	@NonNullable 
	public static <T> StreamErrorLogger<T> create(@NonNullable OutputStream os,
			boolean autoFlush) throws IllegalArgumentException {
		return new StreamErrorLogger<T>(os, autoFlush);
	}
	
	@Override
	public void logInfo(String message) {
		nestedErrorLogger.logInfo(message);
	}

	@Override
	public void logWarning(String message) {
		nestedErrorLogger.logWarning(message);
	}

	@Override
	public void logError(String message) {
		nestedErrorLogger.logError(message);
	}

	@Override
	public void logError(Throwable e) {
		nestedErrorLogger.logError(e);
	}
	
	@Override
	public void logError(ErrorType error) {
		nestedErrorLogger.logError(error.toString());
	}
	
	@Override
	public void logError(String message, Throwable e) {
		nestedErrorLogger.logError(message, e);
	}

	@Override
	public void addError(ErrorType error) {
		nestedErrorLogger.addError(error);
	}

	@Override
	public void logAddError(ErrorType error) {
		nestedErrorLogger.logAddError(error);
	}
	
	@Override
	public void logErrors(Iterable<? extends ErrorType> errors) {
		nestedErrorLogger.logErrors(errors);
	}

	@Override
	public void addErrors(Iterable<? extends ErrorType> errors) {
		nestedErrorLogger.addErrors(errors);
	}

	@Override
	public void logAddErrors(Iterable<? extends ErrorType> errors) {
		nestedErrorLogger.logAddErrors(errors);
	}

	@Override
	public Iterable<ErrorType> getErrors() {
		return nestedErrorLogger.getErrors();
	}

	@Override
	public boolean isErroneous() {
		return nestedErrorLogger.isErroneous();
	}

	@Override
	public void dispose() {
		nestedErrorLogger.dispose();
	}

	@Override
	public OutputStream getOutStream() {
		return nestedLogger.getOutStream();
	}

	@Override
	public void flush() {
		nestedLogger.flush();
	}
}
